from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from mall import settings
def generic_verify_url(user_id,email):

    #1.创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)
    #2.组织数据
    data = {
        'user_id':user_id,
        'email':email
    }
    #3.进行加密处理
    token = serializer.dumps(data)
    #4. 返回
    return 'http://www.meiduo.site:8080/success_verify_email.html?token=%s'%token.decode()


from itsdangerous import BadSignature

def check_active_token(token):

    # 解密

    #1.创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    #2.对数据解密， 解密的时候可能存在异常
    try:
        data = serializer.loads(token)
    except BadSignature:
        return None
    #3.把数据返回回去
    return data


#忘记密码
from itsdangerous import TimedJSONWebSignatureSerializer as tjws
from itsdangerous import BadSignature



def get_tjws_token(username,mobile):

    jws = tjws(secret_key=settings.SECRET_KEY,expires_in=3600)

    data={
        'username':username,
        'mobile':mobile
    }

    token = jws.dumps(data)

    return token.decode()

def decode_jws_token(token):

    jws = tjws(secret_key=settings.SECRET_KEY,expires_in=3600)

    try:
        data = jws.loads(token)
    except BadSignature:
        return None

    return data

def get_password_token(mobile):

    jws = tjws(secret_key=settings.SECRET_KEY,expires_in=3600)

    data={
        'mobile':mobile
    }

    token = jws.dumps(data)

    return token.decode()


def decode_password_token(token):

    jws = tjws(secret_key=settings.SECRET_KEY,expires_in=3600)

    try:
        data = jws.loads(token)
    except BadSignature:
        return None

    return data